package com.example.bookstore.controller;

import com.example.bookstore.entity.BookUser;
import com.example.bookstore.form.userform.LoginForm;
import com.example.bookstore.form.userform.RegisterForm;
import com.example.bookstore.form.userform.ChangePwdForm;
import com.example.bookstore.form.userform.UpdateUserForm;
import com.example.bookstore.service.BookUserService;
import com.example.bookstore.utils.PhoneCode;
import com.example.bookstore.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

@Slf4j
@Api(value = "用户操作接口", tags = {"用户信息管理"})
@RestController
@RequestMapping("/BookUserController")
public class BookUserController {

    @Autowired
    BookUserService bookUserService;

    PhoneCode phoneCode = new PhoneCode();

    @ApiOperation("用户注册")
    @PostMapping("/userRejist")
    public ResultVO UserRejist(@Validated RegisterForm registerForm) throws ParseException {
        return bookUserService.UserRegister(registerForm);
    }

    @ApiOperation("用户更改密码")
    @PostMapping("/UserChangePwd")
    public ResultVO UserChangePwd(@Validated ChangePwdForm changePwdForm) {
        return bookUserService.UserChangePwd(changePwdForm);
    }

    @ApiOperation("用户更改信息")
    @PostMapping("/UpdateUser")
    public ResultVO UpdateUser(@Validated UpdateUserForm updateUserForm) {
        return bookUserService.UpdateUser(updateUserForm);
    }

    @ApiOperation("用户激活")
    @PostMapping("/ActivityUser")
    public ResultVO ActivityUser(String activityCode,Integer userId) {
        return bookUserService.ActivityUser(activityCode, userId);
    }

    @ApiOperation("根据id查询用户")
    @PostMapping("/SelectUserById")
    public ResultVO SelectUserById(Integer userId) {
        return bookUserService.SelectUserById(userId);
    }

    @ApiOperation("根据name模糊查询用户")
    @PostMapping("/SelectUserByName")
    public ResultVO SelectUserByName(String userName) {
        return bookUserService.SelectUserByName(userName);
    }

    @ApiOperation("根据id删除用户")
    @PostMapping("/DeleteUserById")
    public ResultVO DeleteUserById(Integer userId) {
        return bookUserService.DeleteUserById(userId);
    }

    @ApiOperation("用户登录")
    @PostMapping("/UserLogin")
    public ResultVO UserLogin(LoginForm loginForm) {
        return bookUserService.UserLogin(loginForm);
    }

    @ApiOperation("测试获取在线用户")
    @PostMapping("/getCurrentUser")
    public BookUser getCurrentUser() {
        return bookUserService.getCurrentUser();
    }

    @ApiOperation("退出登录")
    @PostMapping("/UserLogout")
    public ResultVO UserLogout() {
        return bookUserService.UserLogout();
    }

    @ApiOperation("获取所有用户")
    @PostMapping("/ShowAllUser")
    public ResultVO ShowAllUser() {
        return bookUserService.ShowAllUser();
    }

    @ApiOperation("发送手机验证码")
    @PostMapping("/SendMsg")
    public ResultVO SendMsg(String telePhone){
        return phoneCode.getPhonemsg(telePhone);
    }

    @ApiOperation("验证六位数验证码登录")
    @PostMapping("/UserLoginByTel")
    public ResultVO UserLoginByTel(String telePhone,String code){
        return bookUserService.UserLoginByTel(telePhone, code);
    }

}
